<?php

namespace app\crontab\controller;

/**
 * 首页接口
 */
class MtyxAqscOrg extends Base
{
	protected $process = 'MtyxAqscOrg';

	public function minglu() {
		$page = $this->request->param('page', 1);
		$curl = $this->loader->loadTool('Curl');
		$post = [
			'search_rows_limit'  => 10,
			'search_post_action' => 'search',
			'order_rule'         => 'default',
			'order_rule_index'   => '-1',
			'eq_dq_province'     => '-999',
			'eq_bu_hz_id'        => '-999',
			'li_productname1'    => '', 
			'bt_yx_dt[]'         => '', 
			'bt_yx_dt[]'         => '' 
		];
		$html = $curl->post('http://mtyx.aqsc.org/Home/Minglu/index/p/' . $page . '.html', $post);
		$dom = new \DOMDocument();
		@$dom->loadHTML($html);
		$xpath = new \DOMXPath($dom);


		$pages = $xpath->query('//*[@class="rows"]')->item(0);
		$str = $pages->textContent;
		$str = explode('当前页', $str)[1];
		$arr = explode('/', $str);
		$page = trim($arr[0]) + 0;
		$totalPage = trim($arr[1]) + 0;



		//$element = $dom->getElementById('dynamic-table');
		$table = $xpath->query('//*[@id="dynamic-table"]')->item(0);
    
	    if (!$table) {
	        return null;
	    }
	    
	    $tbody = $xpath->query('tbody', $table)->item(0);
	    $rows = $xpath->query('tr', $tbody);

	    $data = [];
	    foreach ($rows as $rowIndex => $row) {
	        $cells = $xpath->query('td|th', $row);
	        $colIndex = 0;
	        $a = $xpath->query('a', $cells[9])->item(0);
	        $str = $a->getAttribute('onclick');
	        $str = str_replace('downloadQr(', '', $str);
	        $str = str_replace(')', '', $str);
	        $str = explode(',', $str)[0];
	        $str = str_replace("'", "", $str);
	        $ewm = $curl->post('http://mtyx.aqsc.org/Home/Minglu/downloadQr.html', ['sys_id' => $str]);

	        $arr = [
	        	'province'           => $cells[1]->textContent,
	        	'county'             => $cells[2]->textContent,
	        	'certificate'        => $cells[3]->textContent,
	        	'product_name'       => $cells[4]->textContent,
	        	'certified_unit'     => $cells[5]->textContent,
	        	'production_scale'   => $cells[6]->textContent,
	        	'period_of_validity' => $cells[7]->textContent,
	        	'main_units'         => $cells[8]->textContent,
	        	'ewm'                => 'http://mtyx.aqsc.org/qrcode/qrlogo' . $ewm . '.png'

	        ];

	            // 填充当前单元格
	        $data[$rowIndex] = $arr;

	    }
	    
	    $model = loadModel('MtyxAqscOrgSource');
	    foreach ($data as $k => $row) {
	    	$model->id = null;
	    	$model->isUpdate(false)->save($row);
	    }
	    $domain = $this->request->domain();
	    if($page < $totalPage) {
	    	echo '<script>';
	    	echo 'setTimeout(function(){ location.href="' . $domain . '/crontab/mtyx_aqsc_org/minglu?page=' . ($page + 1) . '"; }, 1000)';
	    	echo '</script>';
	    }
	    /* 
	    echo '<table>';
	    echo '<tr>';
	    echo '<th>ID</th>';
	    echo '<th>身份</th>';
	    echo '<th>县域</th>';
	    echo '<th>证书</th>';
	    echo '<th>产品名称</th>';
	    echo '<th>获证单位</th>';
	    echo '<th>生产规模</th>';
	    echo '<th>有效期</th>';
	    echo '<th>主要生产经营单位</th>';
	    echo '<th>二维码</th>';
	    echo '</tr>';
	    
	    
	    foreach($data as $k => $row) {
	    	echo '<tr>';
	    	echo '<td>' . $k . '</td>';
	    	echo '<td>' . $row['province'] . '</td>';
	    	echo '<td>' . $row['county'] . '</td>';
	    	echo '<td>' . $row['certificate'] . '</td>';
	    	echo '<td>' . $row['product_name'] . '</td>';
	    	echo '<td>' . $row['certified_unit'] . '</td>';
	    	echo '<td>' . $row['production_scale'] . '</td>';
	    	echo '<td>' . $row['period_of_validity'] . '</td>';
	    	echo '<td>' . $row['main_units'] . '</td>';
	    	echo '<td>' . $row['ewm'] . '</td>';
	    	
	    	//
	    	echo '</tr>';
		}
		echo '</table>';
		*/
	}
}
